package com.xm.utils.common;


import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;

public class SignUtil {
    public static byte[] sign(byte[] data, String algorithm, PrivateKey privateKey) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
        Signature instance = Signature.getInstance(algorithm);
        instance.initSign(privateKey);
        instance.update(data);
        return instance.sign();
    }


    public boolean verify(byte[] data, byte[] sign, String algorithm, PublicKey publicKey) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
        Signature instance = Signature.getInstance(algorithm);
        instance.initVerify(publicKey);
        instance.update(data);
        return instance.verify(sign);
    }

}
